<?php
namespace Account;
class FanController extends BaseController{
	public function index(){
		global $G,$lang;
		
		$status = htmlspecialchars($_GET['status']);
		
		$pagesize = 20;
		$totalnum = fan_record_get_num(array('uid'=>$this->uid));
		$pagecount = $totalnum < $pagesize ? 1 : ceil($totalnum/$pagesize);
		$recordlist = fan_record_get_page(array('uid'=>$this->uid), $G['page'], $pagesize);
		$pages = $this->showPages($G['page'], $pagecount, $totalnum);
		
		include template('fan');
	}
	
	public function newticket(){
		global $G,$lang;
		
		if ($this->checkFormSubmit()){
			$sn = htmlspecialchars($_GET['sn']);
			$password = trim($_GET['password']);
			
			if ($sn && $password){
				$ticket = fan_ticket_get_data(array('sn'=>$sn));
				if ($ticket) {
					if ($ticket['password'] !== $password){
						$this->showAjaxError(-1, '卡号和密码不匹配');
					}
					
					if ($ticket['used'] == 1){
						$this->showAjaxError(-1, '卡号已被使用');
					}
					
					//添加排队记录
					fan_record_add_data(array(
							'uid'=>$this->uid,
							'sellerid'=>$ticket['uid'],
							'ticket_sn'=>$ticket['sn'],
							'ticket_amount'=>$ticket['amount'],
							'ticket_complete'=>0,
							'dateline'=>TIMESTAMP
					));
					
					$this->showAjaxReturn(array('result'=>'SUCCESS'));
				}else {
					$this->showAjaxError(-1, '卡号输入错误');
				}
			}else {
				$this->showAjaxError(-1, '卡号密码输入错误');
			}
			
		}else {
			$G['title'] = '添加返券';
			include template('fan_newticket');
		}
	}
}